import VueRouter from 'vue-router'
import store from '@/store/store'
import { Message } from 'view-design'

const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/home/home.vue'),
    redirect: {
      name: 'front'
    },
    children: [
      // 首页
      {
        path: '/front',
        name: 'front',
        component: () => import('@/views/home/front.vue')
      },
      // 产品大厅
      {
        path: '/product',
        name: 'product',
        component: () => import('@/views/product/product.vue')
      },
      {
        path: '/product/product-detail',
        name: 'product-detail',
        component: () => import('@/views/product/product-detail.vue')
      },
      // 竞拍专区
      {
        path: '/bidding',
        name: 'bidding',
        component: () => import('@/views/bidding/bidding.vue')
      },
      {
        path: '/bidding/bidding-detail',
        name: 'bidding-detail',
        component: () => import('@/views/bidding/bidding-detail.vue')
      },
      // 资讯专区
      {
        path: '/news',
        name: 'news',
        component: () => import('@/views/news/news.vue')
      },
      {
        path: '/news/news-detail',
        name: 'news-detail',
        component: () => import('@/views/news/news-detail.vue')
      },
      // 企业公告
      {
        path: '/notice',
        name: 'notice',
        component: () => import('@/views/notice/notice.vue')
      },
      {
        path: '/notice/notice-detail',
        name: 'notice-detail',
        component: () => import('@/views/notice/notice-detail.vue')
      },
      // 个人中心
      {
        path: '/personalCenter',
        name: 'personalCenter',
        component: () => import('@/views/personalCenter/personalCenter.vue'),
        redirect: {
          name: 'user'
        },
        children: [
          {
            path: 'user',
            name: 'user',
            component: () => import('@/views/personalCenter/user.vue')
          },
          {
            path: 'car',
            name: 'car',
            component: () => import('@/views/personalCenter/car.vue')
          },
          {
            path: 'orderList',
            name: 'orderList',
            component: () => import('@/views/personalCenter/orderList.vue')
          },
          {
            path: 'billOrder',
            name: 'billOrder',
            component: () => import('@/views/personalCenter/billOrder.vue')
          },
          {
            path: 'contract',
            name: 'contract',
            component: () => import('@/views/personalCenter/contract.vue')
          },
          {
            path: 'price',
            name: 'price',
            component: () => import('@/views/personalCenter/price.vue')
          },
          {
            path: 'safeSet',
            name: 'safeSet',
            component: () => import('@/views/personalCenter/safeSet.vue')
          }
        ]
      }
    ]
  },
  // 登录、注册
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/login/register.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior: () => ({
    y: 0
  })
})

//不登陆可以访问的页面
const onloginlookeview = [
  '/login',
  '/register',
  '/front',
  '/news',
  '/news/news-detail',
  '/notice',
  '/notice/notice-detail'
]

router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    // 如果未匹配到路由
    Message.warning('无法访问此页面')
    next('/front')
    return
  }
  if (onloginlookeview.indexOf(to.path) !== -1) {
    next()
    return
  } else {
    // console.log('storestore', store);
    if (store.getters.store_token) {
      next()
    } else {
      Message.warning('您现在没有登录，无法访问')
      next('/front')
    }
  }
  // next()
})
export default router
